package com.winHandle.load;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.bean.Field;
import com.bean.Table;
import com.common.FrameUtil;
import com.common.MessageShow;
import com.common.TimeChange;
import com.service.impl.FieldServiceImpl;
import com.service.impl.TableServiceImpl;

public class MainWin_F extends BaseFrame{

	private static final long serialVersionUID = 1409797556587284816L;

	private List<Field> result ;
	private Table tableBean ;

	/** 表单搜索框 */
	private JTextField search_T ;
	/** 字段搜索框 */
	private JTextField search_F ;
	private JButton searchBtn;
	private JButton addBtn;

	private JButton delBtn ;
	private JButton updateBtn ;

	private JTable jTable ;
	private DefaultTableModel model;



	private JPanel top ;
	private JPanel center ;

	public void excute(){

		/***  布局内容   */
		top = new JPanel();
		search_T = new JTextField(15);
		search_F = new JTextField(20);
		searchBtn = new JButton("  查 询  ");
		addBtn = new JButton("  添 加  ");

		search_T.setBounds(10, 60, 67, 25);
		search_F.setBounds(40, 60, 67, 25);

		searchBtn.addActionListener(this);
		addBtn.addActionListener(this);

//		top.add(new JLabel("  "));
		top.add(search_T);
		top.add(search_F);
		top.add(searchBtn);
		top.add(new JLabel("  "));
		top.add(addBtn);

		//top.setLayout(new FlowLayout(FlowLayout.LEFT));
		jframe.getContentPane().add(top,BorderLayout.NORTH);

		center = new JPanel();

		jTable = new JTable();

		String[] titleArr ={"ID","排序","表名","字段名","字段描述"};

		model = new DefaultTableModel();
		model.setColumnIdentifiers(titleArr);
		for(int i = 0 ; i < 15 ; i++){
			model.insertRow(i, new Object[]{});
		}

		jTable.setModel(model);

		JScrollPane scrollpane = new JScrollPane(jTable);
		scrollpane.setPreferredSize(new Dimension(760,350));
		center.add(scrollpane);

		delBtn = FrameUtil.getJButton("  删 除  ", 150, 400, 120, 30);
		updateBtn = FrameUtil.getJButton("  修 改  ", 450, 400, 120, 30);

		jframe.add(delBtn);
		jframe.add(updateBtn);
		delBtn.addActionListener(this);
		updateBtn.addActionListener(this);

		jframe.getContentPane().add(center,BorderLayout.CENTER);

		jframe.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		super.actionPerformed(e);
		String searchStr_T = search_T.getText();
		String searchStr_F = search_F.getText();
		if(e.getSource() == searchBtn){

			if(null == searchStr_T || "".equals(searchStr_T)){
				MessageShow.warn("表单字段不能为空，请填写完整的表单名！");
				return ;
			}
			tableBean = new TableServiceImpl().getByName(searchStr_T);
			if(null == tableBean){
				MessageShow.warn("表单匹配错误，请填写完整的表单名！");
				return ;
			}

			/*if(false && "".equals(searchTXT)){
				MessageShow.warn("请输入要查询的表单名！");
				return ;
			}else{*/
			// 获取数据 更新model
			result = new FieldServiceImpl().loadModelData(model, searchStr_T, searchStr_F);

			//}
		}else if (e.getSource() == addBtn) {
			if(null == searchStr_T || "".equals(searchStr_T)){
				MessageShow.warn("表单字段不能为空，请填写完整的表单名！");
				return ;
			}
			tableBean = new TableServiceImpl().getByName(searchStr_T);
			if(null == tableBean){
				MessageShow.warn("表单匹配错误，请填写完整的表单名！");
				return ;
			}
			new FieldWin("添加字段", model, tableBean, searchStr_F);
		}else if (e.getSource() == delBtn) {

			int rowCount = jTable.getSelectedRowCount() ;
			if(rowCount < 1){
				MessageShow.warn("请选择要删除的记录！");
			}else if (rowCount > 1) {
				MessageShow.warn("每次只能删除一条记录！");
			}else{
				Object selectedCell = model.getValueAt(jTable.getSelectedRow(), 0);
				if(null == selectedCell){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				String id = selectedCell.toString();

				if(0 != MessageShow.confirm("确认要删除ID为 [ " + id + " ] 的记录吗？")){
					return ;
				}
				try {
					if(new FieldServiceImpl().del(Long.parseLong(id))){
						MessageShow.error("删除成功！");
						result = new FieldServiceImpl().loadModelData(model, searchStr_T, searchStr_F);
					}else {
						MessageShow.error("删除失败！");
					}
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					MessageShow.error("参数异常！");
				}

			}
		}else if (e.getSource() == updateBtn) {
			int rowCount = jTable.getSelectedRowCount() ;
			if(rowCount < 1){
				MessageShow.warn("请选择要修改的记录！");
			}else if (rowCount > 1) {
				MessageShow.warn("每次只能操作一条记录！");
			}else{
				Object selectedCell = model.getValueAt(jTable.getSelectedRow(), 0);
				if(null == selectedCell){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				String id = selectedCell.toString();
				if("".equals(id.trim())){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				new FieldWin("字段修改", model, Long.parseLong(id),
						searchStr_T, searchStr_F, true, result);
			}
		}
	}

	public MainWin_F(String title){
		super(title + " -> 字段");
		showFrameName = "FIELD";
		excute();
	}

	public MainWin_F() {
		super();
		showFrameName = "FIELD";
		excute();
	}

}
